<template>
  <x-dialog
    :visible="visible"
    :title="visibleType=='edit'?'编辑':'新增'"
    @close="handleClose(false)"
  >
    <x-page-form ref="pageForm" :form="form" :config="addConfig" :rules="rules" @submit="onSubmit" />
  </x-dialog>
</template>

<script>
import * as axiosApi from '../api'
import { addConfig } from '../config'
import { getClientUserList } from '../../user/api'
import { getSongerList } from '../../songer/api'
import { getSongList } from '../../songlist/api'
export default {
  props: {
    visible: Boolean,
    visibleType: {
      type: String,
      default: ''
    },
    model: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      form: { nickname: '', noType: 'GD' },
      addConfig,
      songlistOptions: [],
      rules: {
        nickname: [
          { required: true, message: '请输选择用户' }
        ],
        noType: [
          { required: true, message: '请输选择评论类型' }
        ],
        modeNo: [
          { required: true, message: '请输选择' }
        ],
        content: [
          { required: true, message: '请输入评论' }
        ]
      }
    }
  },
  watch: {
    visible(newValue, oldValue) {
      if (newValue) {
        Object.keys(this.form).forEach(key => {
          if (key === 'songUrl') {
            this.form[key] = [this.model[key]]
          } else if (this.model[key]) this.form[key] = this.model[key]
        })
      } else {
        this.initForm()
      }
    },
    'form.noType': {
      immediate: true,
      handler(newValue) {
        this.getModeList()
      }
    }
  },
  mounted() {
    this.getUserOptions()
  },
  methods: {
    handleClose(update) {
      this.$emit('close', update)
    },
    // 获取歌手、歌单列表
    getModeList() {
      const fun = this.form.noType === 'GD' ? getSongList : getSongerList
      this.addConfig.formItems[2].label = this.form.noType === 'GD' ? '歌单' : '歌手'
      fun().then(res => {
        this.addConfig.formItems[2].optons = res.data.content.map(item => {
          return {
            label: item.title || item.name,
            value: item.songlistNo || item.songerNo
          }
        })
      })
    },
    // 获取用户列表
    getUserOptions() {
      getClientUserList().then(res => {
        this.addConfig.formItems[0].optons = res.data.content.map(item => {
          return {
            label: item.nickname,
            value: item.userNo
          }
        })
      })
    },
    initForm() {
      Object.keys(this.form).forEach(item => {
        this.form[item] = ''
        if (item === 'songUrl') this.form[item] = []
      })
    },
    onSubmit({ validate }) {
      validate((valid) => {
        if (valid) {
          const params = {
            userNo: this.form.nickname,
            modeNo: this.form.modeNo,
            content: this.form.content
          }
          axiosApi.addComment(params).then(() => {
            this.$message({
              message: '新增成功'
            })
            this.handleClose(true)
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
$dark_gray:#889aa4;
.show-pwd {
    position: absolute;
    right: 10px;
    top: 2px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
</style>
